package com.spzx.product.controller;

import com.spzx.common.core.domain.R;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.log.annotation.Log;
import com.spzx.common.log.enums.BusinessType;
import com.spzx.common.security.annotation.InnerAuth;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.api.domain.Brand;
import com.spzx.product.service.BrandService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.spzx.common.core.web.controller.BaseController;

import java.util.List;

/**
 * <p>
 * 分类品牌 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2024-10-26
 */
@Tag(name = "品牌管理")
@RestController
@RequestMapping("/brand")
public class BrandController extends BaseController {

    @Autowired
    private BrandService brandService;

    @Operation(summary = "分页条件查询品牌列表")
    @GetMapping("/list")
    public TableDataInfo getPageList(String name){
        //开启分页
        startPage();
        List<Brand> brandList=brandService.getPageList(name);
        return getDataTable(brandList);
    }


    @Log(title = "品牌管理",businessType = BusinessType.INSERT)
    @Operation(summary = "添加品牌")
    @PostMapping
    public AjaxResult saveBrand(@RequestBody @Validated Brand brand){
        //设置添加品牌的用户
        brand.setCreateBy(SecurityUtils.getUsername());
        //调用BrandService中的添加方法
        boolean save = brandService.save(brand);
        return toAjax(save);
    }

    @Operation(summary = "根据id查询品牌信息")
    @GetMapping("/{id}")
    //@PathVariable 将url中的模板变量绑定到方法的形参上
    public AjaxResult getBrandById(@PathVariable("id") Long id){
        //调用BranService中根据id查询的方法
        Brand brand = brandService.getById(id);
        return success(brand);
    }

    @Log(title = "品牌管理",businessType = BusinessType.UPDATE)
    @Operation(summary = "根据id更新品牌信息")
    @PutMapping
    public AjaxResult updateBrandById(@RequestBody @Validated Brand brand){
        //设置更新者
        brand.setUpdateBy(SecurityUtils.getUsername());
        //调用BrandService中根据id更新的方法
        boolean update = brandService.updateById(brand);
        return toAjax(update);
    }


    @Operation(summary = "根据id删除品牌信息")
    @DeleteMapping("/{ids}")
    public AjaxResult deleteBrandById(@PathVariable("ids") List<Long> ids){
        //调用BrandService中批量删除的方法
        boolean batch = brandService.removeBatchByIds(ids);
        return toAjax(batch);
    }


    @Operation(summary = "查询所有品牌")
    @GetMapping("/getBrandAll")
    public AjaxResult getBrandAll(){
        List<Brand> brandList = brandService.list();
        return success(brandList);
    }

    @InnerAuth
    @Operation(summary = "获取全部品牌")
    @GetMapping("/getBrandAllList")
    public R<List<Brand>> getBrandAllList() {
        return R.ok(brandService.list());
    }

}
